package GUI.ventas;

import java.awt.BorderLayout;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import DataBase.DAO;


public class Alimentacion extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//private JPanel _panelCentral;

	/**
	 * botones segun la vista
	 */

	private JButton _cero;
	private JButton _uno;
	private JButton _dos;
	private JButton _tres;
	private JButton _cuatro;
	private JButton _cinco;
	private JButton _seis;
	private JButton _siete;
	private JButton _ocho;
	private JButton _nueve;
	private JScrollPane scrollPane;
	private Connection BD;
	private DAO op;
	private Vector<JButton> datos=new Vector<JButton>();
	private JButton[] bo=new JButton[50];
	public static int  inc=0;
	/**
	 * 
	 */

	private Compra tPVQueActualizaTabla;

	public Alimentacion(Connection c) {
		super();

		/**
		 * boton.setBounds(x, y, width, height)
		 */
		//////////////////////
		this.BD=c;
		this.op=new DAO();
		///////////////////
		this.setBounds(800, 1500, 2000, 2000);
		this.construyePanelCentralAlimentos();
		this.setVisible(true);

		this.engineAlimentacion();

	}

	public Alimentacion(Compra t,Connection c) {
		super();
		tPVQueActualizaTabla = t;
		//////////////////////
		this.BD=c;
		this.op=new DAO();
		///////////////////

		/**
		 * boton.setBounds(x, y, width, height)
		 */
		//this.setBounds(500, 120, 1024, 600);
		this.construyePanelCentralAlimentos();
		this.setVisible(true);
		
        /**hay que activarlo**/
		//this.engineAlimentacion();

	}

	private void construyePanelCentralAlimentos() {

		
		//scrollPane = new JScrollPane();
		this.setLayout(new GridLayout(3, 3, 9, 9));
		//_panelCentral.setBorder(new TitledBorder(new EtchedBorder(),
			//	"ALIMENTACI�N"));
		//this.add(scrollPane, BorderLayout.CENTER);
		//scrollPane.add(this);
		//scrollPane.setViewportView(this);
		//scrollPane
			//	.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		// --------------------------------------------------------------
		// a�adimos los productos en este caso son los de alimentacion
		
		
		Statement statement;
		try{
		statement=BD.createStatement();
		ResultSet rs=statement.executeQuery("select id_articulo,nombre from articulo");
		ResultSetMetaData metadata=rs.getMetaData();
		int col=metadata.getColumnCount();

		
		while(rs.next()){
			 
			  int i=0;
				for(int j=1;j<=col;j++){
					j++;
					bo[i]=new JButton(rs.getString(j));
					bo[i].setActionCommand(rs.getString(j));
					this.add(bo[i]);
					bo[i].setIconTextGap(2);
					bo[i].setHorizontalTextPosition(SwingConstants.CENTER);
					bo[i].setVerticalTextPosition(SwingConstants.BOTTOM);
					inc=i;
					bo[i].addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							tPVQueActualizaTabla.anyadirElemento("ID",inc, bo[inc].getText(),1);
						//	System.out.println(""+inc);
							
						}
					});
					i++;
				}
					
		}
		}catch(SQLException e){e.printStackTrace();}

/**		this._cero = new JButton("ARROZ");
		this._cero.setActionCommand("ARROZ");

		this.add(_cero);
		this._cero.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//arroz.png")));
		this._cero.setIconTextGap(2);
		this._cero.setHorizontalTextPosition(SwingConstants.CENTER);
		this._cero.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._uno = new JButton("AT�N");
		this.add(_uno);
		this._uno.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//atun.png")));
		this._uno.setIconTextGap(2);
		this._uno.setHorizontalTextPosition(SwingConstants.CENTER);
		this._uno.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._dos = new JButton("CARNE");
		this.add(_dos);
		this._dos.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//carne.png")));
		this._dos.setIconTextGap(2);
		this._dos.setHorizontalTextPosition(SwingConstants.CENTER);
		this._dos.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._tres = new JButton("COCA COLA");
		this.add(_tres);
		this._tres.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//cocacola.png")));
		this._tres.setIconTextGap(2);
		this._tres.setHorizontalTextPosition(SwingConstants.CENTER);
		this._tres.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._cuatro = new JButton("HUEVOS");
		this.add(_cuatro);
		this._cuatro.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//huevos.png")));
		this._cuatro.setIconTextGap(2);
		this._cuatro.setHorizontalTextPosition(SwingConstants.CENTER);
		this._cuatro.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._cinco = new JButton("LECHE");
		this.add(_cinco);
		this._cinco.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//leche.png")));
		this._cinco.setIconTextGap(2);
		this._cinco.setHorizontalTextPosition(SwingConstants.CENTER);
		this._cinco.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._seis = new JButton("PASTA");
		this.add(_seis);
		this._seis.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//pasta.png")));
		this._seis.setIconTextGap(2);
		this._seis.setHorizontalTextPosition(SwingConstants.CENTER);
		this._seis.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._siete = new JButton("POLLO");
		this.add(_siete);
		this._siete.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//pollo.png")));
		this._siete.setIconTextGap(2);
		this._siete.setHorizontalTextPosition(SwingConstants.CENTER);
		this._siete.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._ocho = new JButton("QUESO");
		this.add(_ocho);
		this._ocho.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//queso.png")));
		this._ocho.setIconTextGap(2);
		this._ocho.setHorizontalTextPosition(SwingConstants.CENTER);
		this._ocho.setVerticalTextPosition(SwingConstants.BOTTOM);

		this._nueve = new JButton("TOMATE");
		this.add(_nueve);
		this._nueve.setIcon(new ImageIcon(this.getClass().getResource(
				".//imagenes//toatefrito.png")));
		this._nueve.setIconTextGap(2);
		this._nueve.setHorizontalTextPosition(SwingConstants.CENTER);
		this._nueve.setVerticalTextPosition(SwingConstants.BOTTOM);**/

		// ---------------------------------------------------------------

	}

	private void engineAlimentacion() {
		
		for(int i=0;i<bo.length;i++){
			
			bo[i].addActionListener(new ActionListener() {
					
				public void actionPerformed(ActionEvent e) {
					tPVQueActualizaTabla.anyadirElemento("ID",1, bo[inc].getText(),1);
					inc++;
				}
			});
			
		}
/**
		_cero.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				tPVQueActualizaTabla.anyadirElemento("ID",1, _cero.getText(), 1);
			}
		});
	
	_uno.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _uno.getText(), 2);
		}
	});
	_dos.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _dos.getText(), 3);
		}
	});
	_tres.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _tres.getText(), 4);
		}
	});
	_cuatro.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _cuatro.getText(), 5);
		}
	});
	_cinco.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _cinco.getText(), 6);
		}
	});
	_seis.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _seis.getText(), 7);
		}
	});
	_siete.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _siete.getText(), 8);
		}
	});
	_ocho.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _ocho.getText(), 9);
		}
	});
	_nueve.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			tPVQueActualizaTabla.anyadirElemento("ID",1, _nueve.getText(), 10);
		}
	});**/
}
	
	

}
